package com.qf.openapi.web.master.interceptor;

import com.qf.openapi.web.master.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//有个特殊的接口
public class ProductInterceptor implements HandlerInterceptor {
    /**
     * 注意，返回值 如果为 true，代表当前请求可以继续向后发送给 Controller中的handler方法
     * 如果返回false，那么当前的请求到此为止
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断用户是否登录过Constants.USER_SESSION_KEY
        final HttpSession session = request.getSession();
        final String user= (String) session.getAttribute("log");
        if (user!=null){ // 用户已登录
            return true;
        }
        // 如果没有登录，则重定向到登录页面去
        // 当访问的页面路径为http://localhost:8080/view/user.html
        // 下面的代码会重定向到如下路径 http://localhost:8080/view/login.html
        response.sendRedirect("/login.html");

        return false;

        }



    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("post~~~~~~");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion~~~~~~");
    }
}
